package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.annotation.Login;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Resource
    private UserInfoService userInfoService;

    @GetMapping("wxLogin/{code}")
    @Operation(summary = "微信授权登录")
    public Result<Map<String, String>> wxLogin(@PathVariable String code) throws WxErrorException {
        Map<String, String> map = userInfoService.wxLogin(code);
        return Result.ok(map);
    }

    @Login
    @GetMapping("getUserInfo")
    @Operation(summary = "获取用户信息")
    public Result<UserInfoVo> getUserInfo() {
        Long userId = AuthContextHolder.getUserId();
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }

    @PostMapping("updateUser")
    @Login
    @Operation(summary = "修改用户信息")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo) {
        userInfoService.updateUser(userInfoVo);
        return Result.ok();
    }
}
